﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Grundy.Client;
using Grundy.Message;
using Grundy.Message.Administration;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Grundy.PerformanceTests
{
    [TestClass]
    public class Client_Load_Tests
    {
        private static LicenseClient _client;
        private static ClientConfiguration _clientConfiguration = ClientConfiguration.Instance;
        const string CUSTOMER_KEY = "95FA6FF2A4C1E8AFBF724C4DACF4A41D1350A1DCA4BBA5F23078632F5A6569E74B283B4E21E0528B9B54FA364787563A6C72310B1582D7F83946CC5A3A07A4EE";

        public TestContext TestContext { get; set; }

        [ClassInitialize()]
        public static void Setup(TestContext testContext)
        {
            _client = new LicenseClient(_clientConfiguration, CUSTOMER_KEY, "Dev_Customer");
        }
        
        [TestMethod]
        public void Checkout_invalid_license()
        {
            var licenseResponse = _client.Checkout(new FeatureIdentificationKey {FeatureId = Guid.NewGuid()});
            Assert.IsFalse(licenseResponse.IsSuccessful, GetRemoteLogMessage(licenseResponse as RemoteLicenseResponse));
        }

        [TestMethod]
        public void Get_licenses_installed_on_server()
        {
            var response = _client.ProcessAdministrationRequest(new GetInstalledLicensesRequest());
            Assert.IsTrue(response is Success);
        }

        [TestMethod]
        public void Checkout_uncounted_floating_license()
        {
            var client = _client;
            var licenseResponse = client.Checkout(new FeatureIdentificationKey { FeatureName = "Dev_Uncounted_Floating" });
            Assert.IsTrue(licenseResponse.IsSuccessful, GetRemoteLogMessage(licenseResponse as RemoteLicenseResponse));
        }

        [TestMethod]
        public void Checkout_and_checkin_counted_floating_concurrent_1000_license()
        {
            var client = _client;
            var licenseResponse = client.Checkout(new FeatureIdentificationKey { FeatureName = "Dev_Counted_Floating_Concurrent_1000" }) as RemoteLicenseResponse;
            Assert.IsTrue(licenseResponse.IsSuccessful, licenseResponse.Message);
            licenseResponse = client.Checkin(licenseResponse) as RemoteLicenseResponse;
            Assert.IsTrue(licenseResponse.IsSuccessful, GetRemoteLogMessage(licenseResponse as RemoteLicenseResponse));
        }

        [TestMethod]
        public void Checkout_counted_floating_executions_1000000000_license()
        {
            var client = _client;
            var licenseResponse = client.Checkout(new FeatureIdentificationKey { FeatureName = "Dev_Counted_Floating_Executions_1000000000" });
            Assert.IsTrue(licenseResponse.IsSuccessful, GetRemoteLogMessage(licenseResponse as RemoteLicenseResponse));
        }

        private string GetRemoteLogMessage(RemoteLicenseResponse response)
        {
            return string.Format("Message: {0} \r\nDetails: {1}", response.Message, response.Details);
        }
    }
}
